<template>
         <div class="login-container">
    <!--
      el-form 表单组件
      每个表单项都必须使用 el-form-item 组件包裹
     -->

    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form class="login-form" ref="form" :model="user" :rules="rules">
        <el-form-item prop="mobile">
          <el-input
            placeholder="请输入手机号"
            v-model="user.mobile"

          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            placeholder="请输入验证码"
            v-model="user.code"

          ></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="user.agree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            @click="getLog"
            :loading="loginLoading"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import axios from 'axios'
export default {
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810',
        agree: true
      },
      loginLoading: false,
      rules: {
        mobile: [
          { required: true, message: '必须要输入手机号', trigger: 'blur' },
          { pattern: /^1[35789]\d{9}$/, message: '手机号格式不对', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '必须要输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '验证码格式不对', trigger: 'blur' }
        ],
        agree: [
          {
            // value: 表示当前值
            // 验证通过： 直接写callback()
            // 验证不通过： callback(new Error(‘错误消息))
            validator: (rule, value, callback) => {
              if (value) {
                // 如是选中，则验证通过
                callback()
              } else {
                // 如是不选中，则给出错误提示通过
                callback(new Error('请同意用户协议'))
              }
            },
            trigger: 'change'
          }
        ]
      }

    }
  },
  methods: {

    getLog () {
      this.loginLoading = true
      axios({
        method: 'post',
        url: 'http://ttapi.research.itcast.cn/mp/v1_0/authorizations',
        data: this.user,
      }).then(res => {
        console.log(res);
        this.$message({
          message: '恭喜你，登陆成功',
          type: 'success'
        })
        this.loginLoading = false
        // 路由跳转
        localStorage.setItem('tokenInfo',res.data.data.token)
        this.$router.push('/')
      }).catch(err => {
        this.$message.error('账号或密码错误')
        this.loginLoading = false
      })
    }

  }
}
</script>

<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
